package com.csx.governance.traffic.adm.model.pojo;

import com.csx.governance.traffic.adm.model.dto.SwimLaneDefinitionDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cuisongxu
 * @date 2023/5/4 14:19
 */

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Schema(description = "发布阶段实体")
public class PublishStage {

    @Schema(description = "阶段类型: INITIAL表示初始阶段,最先执行; TRANSITION表示过渡阶段,中间执行; FINAL表示最终阶段,最后执行")
    private StageTypeEnum type;

    @Schema(description = "泳道规则")
    private String swimLaneRule;

    @Schema(description = "升级策略列表")
    private List<Strategy> upgradeStrategies = new ArrayList<>();

    @Schema(description = "回滚策略列表")
    private List<Strategy> rollbackStrategies = new ArrayList<>();


    @Getter
    public enum StageTypeEnum {

        /**
         * 初始类型,表示最先执行的阶段,只能选择 启用的、稳态类型 的泳道规则
         */
        INITIAL("初始类型"),

        /**
         * 过渡类型,表示中间执行的阶段, 只能选择 过渡类型 的泳道规则
         */
        TRANSITION("过渡类型"),

        /**
         * 最终阶段,表示最后执行的阶段, 只能选择 未启用的、稳态类型 的泳道规则
         */
        FINAL("最终类型")
        ;
        private String name;

        StageTypeEnum(String name) {
            this.name = name;
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Setter
    @Getter
    @Schema(description = "策略实体")
    static class Strategy {

        @Schema(description = "策略名称")
        private String name;

        @Schema(description = "策略类型; K8S_SCRIPT表示K8S上下线脚本类型; BASIC_SWIM_LANE_ADJUST表示基线泳道调整类型")
        private StrategyTypeEnum type;

    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Setter
    @Getter
    @Schema(description = "K8S实例升级策略实体")
    static class K8sStrategy extends Strategy {

        @Schema(description = "K8S服务器地址列表,多个以 逗号 分割")
        private String serverAddresses;

        @Schema(description = "K8S服务器用户名")
        private String username;

        @Schema(description = "K8S服务器密码")
        private String password;

        @Schema(description = "SSH密钥,用于K8S服务器的权限校验")
        private String secret;

        @Schema(description = "脚本")
        private String script;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Setter
    @Getter
    @Schema(description = "基线泳道策略实体")
    static class BaseSwimLaneStrategy extends Strategy {

        @Schema(description = "调整后的基线泳道")
        private SwimLaneDefinitionDTO swimLaneDefinitionDTO;
    }

    @Getter
    public enum StrategyTypeEnum {

        /**
         * K8S实例上下线脚本类型。可以通过将该脚本在K8S服务器上执行从而实现实例上下线
         */
        K8S_SCRIPT("K8S实例上下线脚本"),

        /**
         * 基线泳道调整类型,可以通过调整策略将基线泳道调整
         */
        BASIC_SWIM_LANE_ADJUST("基线泳道调整");

        private String name;

        StrategyTypeEnum(String name) {
            this.name = name;
        }
    }


}
